---
--类名: MakeModel.lua
--创建日期: 2017-12-22
--创建人: 陈小虎
--功能描述:
--修改内容:
--修改日期:
--修改人:
--修改备注:
--
local MakeModel = class("MakeModel")

local string_split = string.split

function MakeModel:ctor()
	cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

	self:clearHasMakeNum()
end

function MakeModel:initMakeInfos()
    local lifeSkills = UD:getLifeSkills()
    -- local formulaIds = UD:getFormulaIds()
    self._makeInfos = {}
    self._showMakeInfos = {}
    self._seqInfos = {}
    local manuC = GD:queryManufacturingSubkeys()
    for k,manu in pairs(manuC) do
    	local id = tonumber(manu.ID)
    	local skillId = id
    	local iconName =  manu.background  
    	local makeInfo = {id = id,nodeMenuLevel = 1,name = L(manu.string),sortLv = tonumber(manu.sortLv),iconPath = GD:getImagePath(Res.ManufacturingIconPath,iconName or ""),subs = {}}

    	local exp,level,maxExp,minLv,cost,unlockFormula = 0,0,0,0,nil,nil
        local lifeSkill = lifeSkills[id]
        if lifeSkill then
            exp = tonumber(lifeSkill.exp) or 0
            level = tonumber(lifeSkill.level) or 0
            local lvC = GD:getManufacturing_levelup(id,lifeSkill.level + 1)
            maxExp = lvC and tonumber(lvC.exp) or 0
            minLv = lvC and tonumber(lvC.minLv) or 0
            cost = lvC and lvC.cost or ""
            unlockFormula = lvC and lvC.unlockFormula or ""
        end

        makeInfo.exp = exp
        makeInfo.maxExp = maxExp
        makeInfo.level = level
        makeInfo.minLv = minLv
        makeInfo.cost = cost
        makeInfo.unlockFormulas = {}

        -- dump(unlockFormula ,"########## unlockFormula ")
        local array = string_split(unlockFormula,";")
        for i,v in ipairs(array) do
        	local sArray = string_split(v,"|")
        	if #sArray >= 2 then
        		-- local classId = tonumber(sArray[1])
        		local fId = tonumber(sArray[2])
        		-- if classId == 0 or classId == UD.player.classID then
        			local fmc = GD:queryFormulaById(fId)
		        	if fmc and fmc.productShow then
		        		local productItem = UD:parseRewardToBagItem(fmc.productShow,"|")
		        		if productItem then
		        			table.insert(makeInfo.unlockFormulas,productItem)
		        		end
		        	end
        		-- end
        	end
        end

        local makeTypeC = GD:queyManufacturingLevelupBySubkey(id)
        if makeTypeC then
        	for _,mk in pairs(makeTypeC) do
        		local mkId = tonumber(mk.ID)
        		local makeTypeInfos = {id = mkId,nodeMenuLevel = 2,
        		manufacturingLv = tonumber(mk.manufacturingLv),
        		name = L(mk.name),
        		subs = {}}

	        	local formulasC = GD:queryFormulaByShowType(mk.ID)
	        	if formulasC then
	        		for _,fm in pairs(formulasC) do
	        			local id = tonumber(fm.ID)
	        			-- local isBelong = false
	        			-- if fm.formulaClass then
	        			-- 	local cArray = string_split(fm.formulaClass,";")
	        			-- 	isBelong = true
		        		-- 	-- for i,v in ipairs(cArray) do
		        		-- 	-- 	local cId = tonumber(v)
		        		-- 	-- 	if cId == 0 then
		        		-- 	-- 		isBelong = true
		        		-- 	-- 		break
		        		-- 	-- 	end
		        		-- 	-- 	-- if UD.player.classID == tonumber(cId) then
		        		-- 	-- 		isBelong = true
		        		-- 	-- 		break
		        		-- 	-- 	-- end
		        		-- 	-- end
		        		-- else
		        		-- 	isBelong = true
	        			-- end
	        			-- if isBelong then
		        			local productItem
							local peifangItem
		        			if fm.productShow then
		        				productItem = UD:parseRewardToBagItem(fm.productShow,"|")
							end

							if fm.itemID then
								peifangItem = UD:parseRewardToBagItem(fm.itemID,"|")
							end

		        			local makeTypeInfo = {
		        			id = id,
		        			nodeMenuLevel = 3,
		        			name = L(fm.name),
		        			skillId = skillId,
		        			getExp = fm.exp,
		        			cost = fm.cost,
		        			quality = fm.quality or 1,
		        			powerCost = fm.powerCost,
		        			limited = fm.limited,
		        			gotoFeature = fm.learn,
		        			manufacturingLv = tonumber(mk.manufacturingLv),
		        			level = makeInfo.level,
		        			sortLv = tonumber(fm.sortLv) or 0,
		        			sourceText = L(fm.sourceText),
		        			productItem = productItem,
							peifangItem = peifangItem
							}
		        			table.insert(makeTypeInfos.subs,makeTypeInfo)
		        			table.insert(self._seqInfos,makeTypeInfo)
	        			-- end
	        		end
			        table.sort(makeTypeInfos.subs,function(a,b)
			            return a.sortLv > b.sortLv
			        end)
	        	end

	        	table.insert(makeInfo.subs,makeTypeInfos)
	        	table.insert(self._seqInfos,makeTypeInfos)
		        table.sort(makeInfo.subs,function(a,b)
		            return a.manufacturingLv > b.manufacturingLv
		        end)
		        if #makeInfo.subs > 0 then
		        	makeInfo.maxLevel = makeInfo.subs[1].manufacturingLv
		        end
        	end

        end
        table.insert(self._makeInfos,makeInfo)
        table.insert(self._seqInfos,makeInfo)
    end
    table.sort(self._makeInfos,function(a,b)
		return a.sortLv > b.sortLv
	end)

    -- dump(self._makeInfos)
    -- dump(self._seqInfos)
    self:filtShowMakeInfos()
end

function MakeModel:getMakeInfos()
	return self._makeInfos
end

function MakeModel:getShowMakeInfos()
	return self._showMakeInfos
end

--过滤没有开启的技能
function MakeModel:filtShowMakeInfos()
	local lifeSkills = UD:getLifeSkills()
	local temUid = {}

	for k,v in pairs(lifeSkills) do
		temUid[k] = 1	
	end
	self._showMakeInfos = {}
	for _,info in pairs(self._makeInfos) do
		if temUid[info.id] then    --该技能开启
			self._showMakeInfos[#self._showMakeInfos + 1] = info
		end			
	end
end

function MakeModel:getInfoByLevelId(nodeLevel,id)
	for i,v in ipairs(self._seqInfos) do
		if v.nodeMenuLevel == nodeLevel then
			if v.id == id then
				return v
			end
		end
	end
	return nil
end

function MakeModel:isPowerEnough(cost)
	return (cost <= UD:getPower()) and true or false
end

function MakeModel:setMakeNum(num)
	self._makeNum = num
end

function MakeModel:getMakeNum()
	return self._makeNum or 0
end

function MakeModel:setMakeData(data)
	self._makeData = data
end

function MakeModel:getMakeData()
	return self._makeData
end

function MakeModel:countHasMakeNum()
	self._hasMakeNum = self._hasMakeNum + 1
end

function MakeModel:getHasMakeNum()
	return self._hasMakeNum
end

function MakeModel:clearHasMakeNum()
	self._hasMakeNum = 0
end

function MakeModel:setAttrBeginningPosition(pos)
	self._attrBeginningPosition = pos
end

function MakeModel:getAttrBeginningPosition()
	return self._attrBeginningPosition
end

function MakeModel:setMakeItemEnough(isEnough)
	self._makeEnough = isEnough
end

function MakeModel:isMakeItemEnough()
	return self._makeEnough
end

function MakeModel:setCurMakeInfo(info)
	self._curMakeInfo = info
end

function MakeModel:getCurMakeInfo()
	return self._curMakeInfo
end

function MakeModel:getCurSkillId()
	local makeInfo = self:getCurMakeInfo()
	local id = makeInfo and makeInfo.id or 0
	return id
end

function MakeModel:setCurFormulaData(data)
	self._curFormulaData = data
end

function MakeModel:getCurFormulaData()
	return self._curFormulaData
end


function MakeModel:getMaxMakeNum()
	local data = self:getMakeData()
	if not data then
		return 0
	end
	local maxLimit = tonumber(data.limited) or 9999
	local makeCostArray = string_split(data.cost,";")
	local iconLimit = 99999
	local mathFloor = math.floor
	local mathMin = math.min
	for k, v in pairs(makeCostArray) do
		local item = UD:parseRewardToBagItem(v,"|")
		local hasNum = UD:getNumByRenderData( item )
		local costNum = item.amount
		local num = mathFloor(hasNum/costNum)
		iconLimit = mathMin(num, iconLimit)
	end
	maxLimit = mathMin(iconLimit, maxLimit)

	local hasPower = UD:getPower()
	local powerLimit = mathFloor(hasPower / data.powerCost)
	maxLimit = mathMin(powerLimit, maxLimit)
	return maxLimit
end

function MakeModel:C2S_Composite()
    local data = self:getCurFormulaData()
    if data then
	    app.session:request("action.C2S_Composite",{formulaId = data.id, num = self:getMakeNum()},function(msg)
	     	if msg and msg.ok then
	     		self:initMakeInfos()
	     		display.pushToast(L("formulaComplete"))
	     		self:dispatchEvent({name = "onMakeCompleted",ok = true})
	     	else
	     		self:dispatchEvent({name = "onMakeCompleted",ok = false})
	     	end
	    end,{block = false},function(msg)
	    	if msg and msg.content ~= nil then
	    		self:dispatchEvent({name = "onMakeCompleted",ok = false})
	    	end
	    end)
    end
end

function MakeModel:C2S_UpgradeLifeSkill()
    local info = self:getCurMakeInfo()
    if info then
	    app.session:request("action.C2S_UpgradeLifeSkill",{addLevel = 1,uid = info.id},function(msg)
	    	self:initMakeInfos()
	    	display.pushToast(L("tips_1070002"))
	    	self:dispatchEvent({name = "onLifeLiveUpCompleted",ok = true})
	    end,{block = false},function(msg)
	    	if msg and msg.content ~= nil then
	    		self:dispatchEvent({name = "onLifeLiveUpCompleted",ok = false})
	    	end
	    end)
    end
end


return MakeModel
